<template>
  <div v-if="loginType == 2" class="c-h c-pt160 c-flex-column c-aligni-center">
    <i class="iconfont c-fc-smblue c-fs100">&#xe6eb;</i>
    <div class="c-fs34 c-fc-smblue c-fw600 c-pt32">登录成功</div>
    <div class="c-pt10 c-fs-sblack c-fs22 c-pb120">恭喜您，成功登录创客匠人桌面助手</div>
    <div class="btn-konw" @click="closeWindow">我知道了</div>
  </div>
  <div v-else class="c-h c-w100 c-ph48 c-pt60">
    <img :src="$addXossFilter(shareLogo, require('../../../assets/defult_head.png'))" class="c-ww100 c-hh100 c-brp50 c-bd1 c-center" />
    <div class="c-fs30 c-mt10 c-textAlign-c">{{companyName}}</div>
    <div class="c-mt40 c-mb20 line"></div>
    <div class="c-fs24 c-fc-xblack c-mt20">即将登陆{{companyName}}PC版，请确认是本人操作</div>
    <div class="c-fs20 c-fc-gray c-mt10 c-mb20">使用您的账号登录该应用</div>
    <div class="c-bg-mgreen c-fc-white c-fs30 c-pv24 c-w100 c-br6 c-textAlign-c c-mt40" @click="pcLogin">确认登录</div>
    <div class="c-fc-xblack c-bg-sgray c-bd1-mgray c-fs30 c-pv24 c-w100 c-br6 c-textAlign-c c-mt20" @click="closeWindow">取消登录</div>
  </div>
</template>

<script>
import { utilJs } from "@/utils/common.js"
export default {
  name: 'PCLogin',
  components: {

  },
  props: [],
  beforeCreate() {

  },
  data() {
    return {
      shareLogo: localStorage.getItem("shareLogo"),
      companyName: localStorage.getItem("mpName"),
      sess: '',
      domain: '',
      loginType: 1,
    }
  },
  created() {
    //pc端扫描二维码进行登录
    setDocumentTitle("微信登录");// eslint-disable-line
    this.sess = this.$route.query.sess;
    this.domain = this.$route.query.d;
    this.loginType = this.$route.query.loginType || 1;
    this.$store.commit('updateLoadingStatus', {
      isLoading: false
    });
  },
  mounted() {

  },
  computed: {

  },
  watch: {

  },
  methods: {
    pcLogin() {
      if (this.$route.query.scene == 1) {
        this.liveAdminLogin();
        return;
      }
      if (this.$route.query.liveId) {
        this.liveLogin();
      } else {
        this.pcwxLogin();
      }
    },
    liveAdminLogin() {
      let rand = this.$route.query.rand;
       let url = `${global.apiurl}livePcWebCenter/authLogin`;
      utilJs.postMethod(url, {rand:rand}, res => {
        this.closeWindow();
      }, failRes => {
        this.$showCjToast({
          text: failRes.data.msg || "登录失败，请重新扫码登录！",
          type: "warn",
        });
      })
    },
    liveLogin() {
      let type = this.$route.query.type || 1;
      let url = `${global.apiurl}liveFlow/checkManager/${this.$route.query.liveId}/${this.$route.query.rand}?type=${type}`;
      utilJs.getMethod(url, res => {
        let teacherText = type == 2 ? '讲师/助教' : '讲师';
        if (res.status == 0) {
          let tipTitle = res.liveType == 5 ? 'PC端视频直播间只支持讲师一人推流，其他身份请登录公众号直播间！' : res.liveType == 6 ? `PC端直播间只支持${teacherText}登录，请确认登录的身份！` : `你不是管理员或${teacherText}身份，无法登录！`
          this.$showCjToast({
            text: tipTitle,
            type: "warn",
          });
        } else {
          this.closeWindow();
        }
      }, failRes => {
        this.$showCjToast({
          text: "登录失败，请重新扫码登录！",
          type: "warn",
        });
      });
    },
    pcwxLogin: function () {
      //通过host的不同，向后台请求时链接不同
      let $this = this;
      utilJs.postMethod(
        global.apiurl + "pc/login/qrcodeLogin",
        {
          sess: this.sess,
          c: localStorage.getItem('companyId'),
          u: localStorage.getItem('userId'),
          t: localStorage.getItem('token')
        },
        function (res) {
          $this.closeWindow();
        },
        function () {
          $this.$showCjToast({
            text: "登录失败，请重新扫码登录！",
            type: "warn",
          });
        }
      );
    },
    //确认登录后自动关闭当前页面
    closeWindow: function (event) {
      window.WeixinJSBridge.call('closeWindow');
    }
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.line {
  height: 1px;
  background-color: #999;
  width: 100%;
}
.btn-konw{
  width: 12rem;
  height: 1.8rem;
  border-radius: 0.9rem;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 0.7rem;
  color: #fff;
  background: #4c92fc;
}
</style>
